<div class="layui-fluid layui-anim febs-anim" id="febs-warehouseHouse" lay-title="仓库管理"
     xmlns:shiro="http://www.w3.org/1999/xhtml">
  <div class="layui-row febs-container">
    <div class="layui-col-md12">
      <div class="layui-card">
        <div class="layui-card-body febs-table-full">
          <form class="layui-form layui-table-form" lay-filter="warehouseHouse-table-form">
            <div class="layui-row">
              <div class="layui-col-md10">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <label class="layui-form-label layui-form-label-sm">仓库名称</label>
                    <div class="layui-input-inline">
                      <input type="text" name="housename" autocomplete="off" class="layui-input">
                    </div>
                  </div>
                </div>
              </div>
              <div class="layui-col-md2 layui-col-sm12 layui-col-xs12 table-action-area">
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="query">
                  <i class="layui-icon">&#xe848;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action" id="reset">
                  <i class="layui-icon">&#xe79b;</i>
                </div>
                <div class="layui-btn layui-btn-sm layui-btn-primary table-action action-more"
                     shiro:hasAnyPermissions="warehouseHouse:view">
                  <i class="layui-icon">&#xe875;</i>
                </div>
              </div>
            </div>
          </form>
          <table lay-filter="warehouseHouseTable" lay-data="{id: 'warehouseHouseTable'}"></table>
        </div>
      </div>
    </div>
  </div>
</div>
<script type="text/html" id="warehouse-book" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="warehouseHouse:view">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="see" shiro:hasPermission="warehouseHouse:view"><i
          class="layui-icon febs-edit-area febs-green">&#xe7a5;</i></a>
</script>
<script type="text/html" id="warehouseHouse-option" xmlns:shiro="http://www.w3.org/1999/xhtml">
  <span shiro:lacksPermission="warehouseHouse:view">
        <span class="layui-badge-dot febs-bg-orange"></span>无权限
    </span>
  <a lay-event="edit" shiro:hasPermission="warehouseHouse:update"><i
          class="layui-icon febs-edit-area febs-blue">&#xe7a4;</i></a>
  <a lay-event="del" shiro:hasPermission="warehouseHouse:delete">
    <i class="layui-icon febs-edit-area febs-red">&#xe7f9;</i></a>
  <a lay-event="export" shiro:hasPermission="warehouseHouse:update">
    <i class="layui-icon febs-edit-area febs-orange">&#xe62d;</i></a>
</script>
<script data-th-inline="none" type="text/javascript">
  layui.use(['dropdown', 'jquery', 'form', 'table', 'febs'], function () {
    var $ = layui.jquery,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        dropdown = layui.dropdown,
        $view = $('#febs-warehouseHouse'),
        $query = $view.find('#query'),
        $reset = $view.find('#reset'),
        $searchForm = $view.find('form'),
        sortObject = {field: '', type: ''},
        tableIns;

    form.render();

    initTable();

    dropdown.render({
      elem: $view.find('.action-more'),
      click: function (name, elem, event) {
        var checkStatus = table.checkStatus('warehouseHouseTable');
        if (name === 'add') {
          febs.modal.open('新增信息', 'warehouse/warehouseHouse/add', {
            btn: ['提交', '重置'],
            area: $(window).width() <= 750 ? '95%' : '50%',
            yes: function (index, layero) {
              $('#warehouseHouse-add').find('#submit').trigger('click');
            },
            btn2: function () {
              $('#warehouseHouse-add').find('#reset').trigger('click');
              return false;
            }
          });
        }
        if (name === 'delete') {
          if (!checkStatus.data.length) {
            febs.alert.warn('请选择需要删除的记录');
          } else {
            febs.modal.confirm('删除信息', '确定删除该条？', function () {
              var warehouseHouseIds = [];
              layui.each(checkStatus.data, function (key, item) {
                warehouseHouseIds.push(item.houseid)
              });
              deletewarehouseHouses(warehouseHouseIds.join(','));
            });
          }
        }
      },
      options: [{
        name: 'add',
        title: '新增仓库',
        perms: 'warehouseHouse:add'
      }, {
        name: 'delete',
        title: '删除仓库',
        perms: 'warehouseHouse:delete'
      }]
    });

    table.on('tool(warehouseHouseTable)', function (obj) {
      var data = obj.data,
          layEvent = obj.event;
      if (layEvent === 'export') {
        var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
        febs.download(ctx + 'warehouseBookList/excel/' + data.houseid, params, '仓库详情.xlsx');
      }
      if (layEvent === 'del') {
        febs.modal.confirm('删除书籍信息', '确定删除该书籍信息？', function () {
          deletewarehouseHouses(data.houseid);
        });
      }
      if (layEvent === 'see') {
        febs.modal.open('仓库详情', 'warehouse/warehouseBook/see/' + data.houseid, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['关闭'],
          btn2: function () {
            layer.closeAll();
          }
        });
      }
      if (layEvent === 'edit') {
        febs.modal.open('修改书籍信息', 'warehouse/warehouseHouse/update/' + data.houseid, {
          area: $(window).width() <= 750 ? '90%' : '50%',
          btn: ['提交', '取消'],
          yes: function (index, layero) {
            $('#warehouseHouse-update').find('#submit').trigger('click');
          },
          btn2: function () {
            layer.closeAll();
          }
        });
      }
    });

    table.on('sort(warehouseHouseTable)', function (obj) {
      sortObject = obj;
      tableIns.reload({
        initSort: obj,
        where: $.extend(getQueryParams(), {
          field: obj.field,
          order: obj.type
        })
      });
    });

    $query.on('click', function () {
      var params = $.extend(getQueryParams(), {field: sortObject.field, order: sortObject.type});
      tableIns.reload({where: params, page: {curr: 1}});
    });

    $reset.on('click', function () {
      $searchForm[0].reset();
      tableIns.reload({where: getQueryParams(), page: {curr: 1}, initSort: sortObject});
    });

    febs.editTable('warehouseHouseTable', 'warehouseHouse/update/');

    function initTable() {
      tableIns = febs.table.init({
        elem: $view.find('table'),
        id: 'warehouseHouseTable',
        url: ctx + 'warehouseHouse/list',
        cols: [[
          {type: 'checkbox'},
          {title: '仓库名称', field: 'housename', edit: 'text', minWidth: 100},
          {title: '仓库容量', field: 'housenumber', edit: 'text'},
          {title: '储书数量', field: 'housebooks'},
          {title: '查看详情', templet: '#warehouse-book'},
          {title: '操作', toolbar: '#warehouseHouse-option', minWidth: 140}
        ]]
      });
    }

    function getQueryParams() {
      return {
        housename: $searchForm.find('input[name="housename"]').val().trim(),
      };
    }

    function deletewarehouseHouses(warehouseHouseIds) {
      febs.post(ctx + 'warehouseHouse/delete/' + warehouseHouseIds, null, function () {
        febs.alert.success('删除成功');
        $query.click();
      });
    }
  })
</script>